/******************************************************************
 * Copyright(c)  Suzsoft DotNet Group
 * Description   : 
 * CreateDate    : 2006-06-05 
 * Creater       : Brian.xue
 * LastChangeDate: 
 * LastChanger   : 
 * Version Info  : 1.0
 * ******************************************************************/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;


namespace WorkflowDesigner
{
    //All Coordinates passed in physical coordinate system
    //Some of the functions will have coordinates in screen coordinates ie ShowContextMenu
    internal sealed class CustomMessageFilter : WorkflowDesignerMessageFilter
    {
        #region Members and Contructor

        private bool mouseDown;
        private IServiceProvider serviceProvider;
        private WorkflowView workflowView;

        public CustomMessageFilter(IServiceProvider provider, WorkflowView workflowView)
        {
            serviceProvider = provider;
            this.workflowView = workflowView;
        }

        #endregion

        #region MessageFilter Overridables

        protected override bool OnMouseDown(MouseEventArgs eventArgs)
        {
            //Allow other components to process this event by not returning true.
            mouseDown = true;
            return false;
        }

        protected override bool OnMouseMove(MouseEventArgs eventArgs)
        {
            //Allow other components to process this event by not returning true.
            if (mouseDown)
            {
                workflowView.ScrollPosition = new Point(eventArgs.X, eventArgs.Y);
            }
            return false;
        }

        protected override bool OnMouseUp(MouseEventArgs eventArgs)
        {
            //Allow other components to process this event by not returning true.
            mouseDown = false;
            return false;
        }

        protected override bool OnMouseDoubleClick(MouseEventArgs eventArgs)
        {
            mouseDown = false;
            return true;
        }

        protected override bool OnMouseEnter(MouseEventArgs eventArgs)
        {
            //Allow other components to process this event by not returning true.
            mouseDown = false;
            return false;
        }

        protected override bool OnMouseHover(MouseEventArgs eventArgs)
        {
            //Allow other components to process this event by not returning true.
            mouseDown = false;
            return false;
        }

        protected override bool OnMouseLeave()
        {
            //Allow other components to process this event by not returning true.
            mouseDown = false;
            return false;
        }

        protected override bool OnMouseWheel(MouseEventArgs eventArgs)
        {
            mouseDown = false;
            return true;
        }

        protected override bool OnMouseCaptureChanged()
        {
            //Allow other components to process this event by not returning true.
            mouseDown = false;
            return false;
        }

        protected override bool OnDragEnter(DragEventArgs eventArgs)
        {
            return true;
        }

        protected override bool OnDragOver(DragEventArgs eventArgs)
        {
            return true;
        }

        protected override bool OnDragLeave()
        {
            return true;
        }

        protected override bool OnDragDrop(DragEventArgs eventArgs)
        {
            return true;
        }

        protected override bool OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
        {
            return true;
        }

        protected override bool OnQueryContinueDrag(QueryContinueDragEventArgs qcdevent)
        {
            return true;
        }

        protected override bool OnKeyDown(KeyEventArgs eventArgs)
        {
            if (eventArgs.KeyCode == Keys.Delete)
            {
                ISelectionService selectionService = (ISelectionService)serviceProvider.GetService(typeof(ISelectionService));
                if (selectionService != null && selectionService.PrimarySelection is Activity)
                {
                    if (MessageBox.Show("Are you sure to delete the activity?", "Confirm", MessageBoxButtons.YesNo)
                        == DialogResult.Yes)
                    {
                        Activity ActivityComponent = (Activity)selectionService.PrimarySelection;
                        CompositeActivity parentActivity = ActivityComponent.Parent;
                        if (parentActivity != null)
                        {
                            parentActivity.Activities.Remove(ActivityComponent);
                            this.ParentView.Update();
                        }
                    }  
                }
            }
            return true;
        }

        protected override bool OnKeyUp(KeyEventArgs eventArgs)
        {
            return true;
        }

        protected override bool OnShowContextMenu(Point menuPoint)
        {
            return true;
        }

        #endregion
    }

}
